home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 58844 / 58844.xpi / chrome / content / wordschinesepopupdictionary.js < prev   
Text File  |  2010-01-27  |  15KB  |  452 lines

  1. var wordschinesepopupdictionary = {
  2.     maxLength: 200,
  3.     mouseDownX: 0,
  4.     mouseDownY: 0,
  5.     divId: "wc-transdiv",
  6.     ifrId: "wc-transifr",
  7.     url: "http://www.words-chinese.com/searchCnTextFFEX.php",
  8.     isInsidePanel: false,
  9.     isControlPressed: false,
  10.     isActivated: true,
  11.     isCN: true,
  12.     retWC: "",
  13.     retGG: "",
  14.     userId: "",
  15.     langId: "en",
  16.     currDoc: null,
  17.     //--------------------------------------------------------
  18.  
  19.     activate: function() {
  20.         wordschinesepopupdictionary.isActivated=!wordschinesepopupdictionary.isActivated;
  21.         wordschinesepopupdictionary.showIcon(wordschinesepopupdictionary.isActivated);
  22.     },
  23.     
  24.  
  25.     showIcon: function(isVisible) {
  26.         if(isVisible){
  27.             //Activated
  28.             try{
  29.                 document.getElementById("wordschinesepopupdictionary-statusicon").setAttribute("src", "chrome://wordschinesepopupdictionary/content/icon1.png");
  30.             }catch(e){alert(e);}
  31.         }else{
  32.             //Deactivated
  33.             try{
  34.                 document.getElementById("wordschinesepopupdictionary-statusicon").setAttribute("src", "chrome://wordschinesepopupdictionary/content/icon2.png");
  35.             }catch(e){alert(e);}
  36.         }
  37.     },
  38.     
  39.     showAbout: function() {
  40.         alert("Words-Chinese Pinyin Dictionary, Created by Huang Jason. \nGet Chinese translation by double-click, or rightward and downward selection.\n\n");
  41.     },
  42.  
  43.     showHelp: function() {
  44.         alert("Help:\n\nTo de/activate the dictionary ->\n\n1.Click the [w] icon in the Status Bar Panel\n2.Right click the [w] icon. \n\nTo show translation & definition ->\n\n1. Double-click \n2. Make selection rightward and downward.\n\n");
  45.     },
  46.  
  47.     goHome: function() {
  48.         var win = window.top.getBrowser().selectedBrowser.contentWindow;
  49.         win.open("http://www.words-chinese.com/");
  50.     },
  51.     
  52.     bugReport: function(){
  53.         var win = window.top.getBrowser().selectedBrowser.contentWindow;
  54.         win.open("http://www.words-chinese.com/contact");
  55.     },
  56.  
  57.     openHistory: function(){
  58.         if(wordschinesepopupdictionary.userId){
  59.             var win = window.top.getBrowser().selectedBrowser.contentWindow;
  60.             win.open("http://www.words-chinese.com/historydict?user="+wordschinesepopupdictionary.userId);
  61.         }else{
  62.             wordschinesepopupdictionary.changeUser();
  63.             wordschinesepopupdictionary.openHistory();
  64.         }
  65.     },
  66.     
  67.     changeUser: function(){
  68.         var inputText = "none";
  69.         if(wordschinesepopupdictionary.userId){
  70.             inputText=wordschinesepopupdictionary.userId;
  71.         }
  72.         var answer = prompt ("To store your search history online. Please provide an email address as your unique ID: (Use [none] to remove it.)", inputText);
  73.         
  74.         if(!answer){
  75.             alert ("You ID/email is not correct.");
  76.         }else if(answer=="none"){
  77.             wordschinesepopupdictionary.userId="";
  78.         }else if(answer.length>8 && answer.indexOf(".")>-1 && answer.indexOf("@")>-1 && answer.indexOf("&")==-1 && answer.indexOf("!")==-1 && answer.indexOf("#")==-1 && answer.indexOf("$")==-1 && answer.indexOf("%")==-1 && answer.indexOf("^")==-1 && answer.indexOf("*")==-1 && answer.indexOf("(")==-1 && answer.indexOf(")")==-1 && answer.indexOf("+")==-1 && answer.indexOf("/")==-1){
  79.             wordschinesepopupdictionary.userId=answer;
  80.         }else{
  81.             alert ("You ID/email is not correct.");
  82.         }
  83.     },
  84.  
  85.     chooseLanguage: function(languageId){
  86.         wordschinesepopupdictionary.langId=languageId;
  87.     },
  88.     
  89.     showMenu: function(){
  90.         //modify ID
  91.         try{
  92.             var eleId="wordschinesepopupdictionary-lang-";
  93.             eleId+=wordschinesepopupdictionary.langId;
  94.             document.getElementById(eleId).setAttribute("checked", "true");
  95.         }catch(e){alert(e);}
  96.         
  97.     },
  98.     
  99.     
  100.     //--------------------------------------------------------
  101.     loadOptions: function() {
  102.         try{
  103.             // Get the "extensions.myext." branch
  104.             var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.wordschinesedict.");
  105.  
  106.             if (prefs.prefHasUserValue("selectedLanguageId")) {
  107.                 wordschinesepopupdictionary.langId=prefs.getCharPref("selectedLanguageId");
  108.                 if(wordschinesepopupdictionary.langId=="")
  109.                 {
  110.                     wordschinesepopupdictionary.langId="en";    
  111.                 }
  112.             }
  113.             
  114.             if (prefs.prefHasUserValue("isActivated")) {
  115.                 wordschinesepopupdictionary.isActivated=prefs.getBoolPref("isActivated");
  116.             }
  117.             
  118.             if (prefs.prefHasUserValue("userId")) {
  119.                 wordschinesepopupdictionary.userId=prefs.getCharPref("userId");
  120.             }
  121.             
  122.             wordschinesepopupdictionary.showIcon(wordschinesepopupdictionary.isActivated);
  123.             
  124.  
  125.         }catch(e){alert(e);}
  126.     },
  127.  
  128.     saveOptions: function() {
  129.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.wordschinesedict.");
  130.         prefs.setCharPref("selectedLanguageId",wordschinesepopupdictionary.langId);
  131.         prefs.setBoolPref("isActivated",wordschinesepopupdictionary.isActivated);
  132.         prefs.setCharPref("userId",wordschinesepopupdictionary.userId);
  133.     },
  134.     
  135.     //--------------------------------------------------------
  136.     load: function() {
  137.         // initialization code
  138.         this.initialized = true;
  139.         // Add listeners
  140.         gBrowser.addEventListener("dblclick", wordschinesepopupdictionary.CheckClick, false);
  141.         gBrowser.addEventListener("mousedown", wordschinesepopupdictionary.CheckClick, false);
  142.         gBrowser.addEventListener("mouseup", wordschinesepopupdictionary.CheckClick, false);
  143.         wordschinesepopupdictionary.loadOptions();
  144.     },
  145.  
  146.     
  147.     unload: function()
  148.     {
  149.         // Remove listeners
  150.         gBrowser.removeEventListener("dblclick", wordschinesepopupdictionary.CheckClick, false);
  151.         gBrowser.removeEventListener("mousedown", wordschinesepopupdictionary.CheckClick, false);
  152.         gBrowser.removeEventListener("mouseup", wordschinesepopupdictionary.CheckClick, false);
  153.         wordschinesepopupdictionary.saveOptions();
  154.     },
  155.     
  156.     
  157.     CheckClick: function(e)
  158.     {
  159.         if(!wordschinesepopupdictionary.isActivated){
  160.             return;
  161.         }
  162.  
  163.         if(wordschinesepopupdictionary.isInsidePanel){
  164.             return;
  165.         }
  166.  
  167.  
  168.         // Type of event; kill panel
  169.         if (e.type=="dblclick"){
  170.             wordschinesepopupdictionary.KillPanel();
  171.         }else if (e.type=="mouseup"){
  172.         }else{
  173.             wordschinesepopupdictionary.KillPanel();
  174.         }
  175.  
  176.         wordschinesepopupdictionary.currDoc=null;
  177.         
  178.         var docCount=0;
  179.         var el = e.target;
  180.         if (!el) el = e.srcElement;
  181.         
  182.         while (el){
  183.             if (el.nodeName=="#document"){
  184.                 docCount+=1;
  185.                 if(docCount=1){
  186.                     wordschinesepopupdictionary.currDoc=el;
  187.                 }
  188.             }
  189.             el = el.parentNode;
  190.         }
  191.  
  192.  
  193.  
  194.         var posx = 0;
  195.         var posy = 0;
  196.         var cursorSize = 5;
  197.         var margin = 10;
  198.         
  199.  
  200.         //cursor position
  201.         if (e.clientX || e.clientY){
  202.             var doc = wordschinesepopupdictionary.currDoc;
  203.             if(!doc) doc=content.document;
  204.             posx = e.clientX + doc.body.scrollLeft + doc.documentElement.scrollLeft;
  205.             posy = e.clientY + doc.body.scrollTop + doc.documentElement.scrollTop;
  206.         }else if (e.pageX || e.pageY){
  207.             posx = e.pageX;
  208.             posy = e.pageY;
  209.         }
  210.  
  211.         
  212.         //cursor size
  213.         posx += cursorSize;
  214.         posy += cursorSize;
  215.  
  216.         // Type of event
  217.         if (e.type=="dblclick"){
  218.             //pass
  219.         }else if (e.type=="mouseup"){
  220.             if(wordschinesepopupdictionary.mouseDownX+margin<posx || wordschinesepopupdictionary.mouseDownY+margin<posy){
  221.                 //pass
  222.             }else{
  223.                 return;
  224.             }
  225.         }else{
  226.             wordschinesepopupdictionary.mouseDownX=posx;
  227.             wordschinesepopupdictionary.mouseDownY=posy;
  228.             return;
  229.         }
  230.  
  231.  
  232.         // Gets the selected text
  233.         var sel = getBrowserSelection();
  234.  
  235.         // Checks for selected text
  236.         if (sel)
  237.         {
  238.             //trim
  239.             sel=sel.replace(/^\s*/, "").replace(/\s*$/, "");
  240.             if(sel!=""){
  241.                 //cut string length
  242.                 var strCount = sel.length;
  243.                 if (strCount>wordschinesepopupdictionary.maxLength){
  244.                     sel=sel.substr(0,wordschinesepopupdictionary.maxLength);
  245.                 }
  246.  
  247.                 wordschinesepopupdictionary.CreatePanel(sel,posx,posy,doc);
  248.                 wordschinesepopupdictionary.Translate(sel);
  249.             }
  250.         }
  251.     },
  252.  
  253.  
  254.  
  255.     CreatePanel: function(text,x,y) {
  256.         var doc=wordschinesepopupdictionary.currDoc;
  257.         if(!doc) doc= content.document;
  258.  
  259.  
  260.         var div = doc.body.appendChild(doc.createElement("div"));
  261.         div.setAttribute("style",
  262.         "position: absolute; width: auto; height: auto;"
  263.         +"left: "+ x +"px; top: "+ y +"px; "
  264.         +"border: 2px solid #409FFF; background-color: #F9F9F9; z-index:100000;"
  265.         +"padding-left: 4px; padding-right: 4px; padding-top: 4px; padding-bottom: 4px;"
  266.         +"-moz-border-radius-topright: 15px;-webkit-border-top-right-radius: 15px;-moz-border-radius-bottomleft: 15px;-webkit-border-bottom-left-radius: 15px;-moz-border-radius-bottomright: 15px;-webkit-border-bottom-right-radius: 15px;"
  267.         );
  268.         div.setAttribute("id",wordschinesepopupdictionary.divId);
  269.         div.setAttribute("align","left");
  270.         div.innerHTML=text;
  271.  
  272.         div.addEventListener("mouseout",wordschinesepopupdictionary.OnMouseOut, false);
  273.         div.addEventListener("mouseover",wordschinesepopupdictionary.OnMouseOver, false);
  274.  
  275.     },
  276.  
  277.     OnMouseOut: function() {
  278.         wordschinesepopupdictionary.isInsidePanel=false;
  279.     },
  280.  
  281.     OnMouseOver: function(e) {
  282.         
  283.         wordschinesepopupdictionary.isInsidePanel=true;
  284.         
  285. //        var el = e.target;
  286. //        if (!el) el = e.srcElement;
  287.     },
  288.  
  289.     KillPanel: function() {
  290.         var doc = wordschinesepopupdictionary.currDoc;
  291.         if(!doc) doc=content.document;
  292.         var div = doc.getElementById(wordschinesepopupdictionary.divId);
  293.         if(div){
  294.  
  295.             div.removeEventListener("mouseout",wordschinesepopupdictionary.OnMouseOut, false);
  296.             div.removeEventListener("mouseover",wordschinesepopupdictionary.OnMouseOver, false);
  297.  
  298.             div.parentNode.removeChild(div);
  299.         }
  300.         wordschinesepopupdictionary.isInsidePanel=false;
  301.     },
  302.  
  303.     isChinese: function(temp)
  304.     {
  305.         var r = /[\u4e00-\u9fa5]/;
  306.         if(r.test(temp)){
  307.             return true;
  308.         }else{
  309.             return false;
  310.         }
  311.     },
  312.     
  313.  
  314.     Translate: function(text){
  315.         var target = "";
  316.         var user = "";
  317.         var iscn = wordschinesepopupdictionary.isChinese(text);
  318.         
  319.         wordschinesepopupdictionary.isCN = iscn;
  320.         wordschinesepopupdictionary.retWC = "";
  321.         wordschinesepopupdictionary.retGG = "";
  322.         
  323.         if(iscn){
  324.             target=wordschinesepopupdictionary.langId;
  325.             var url = wordschinesepopupdictionary.url+"?hs=1&lang="+target+"&uid="+user+"&q="+text;
  326.             url=url.replace("q=#","q=");
  327.             wordschinesepopupdictionary.doRequest(url);
  328.             wordschinesepopupdictionary.googleTranslate(text,target);
  329.         }else{
  330.             target = "zh-CN";
  331.             wordschinesepopupdictionary.googleTranslate(text,target);
  332.         }
  333.     },
  334.  
  335.     setDiv: function(){
  336.             var style="<style>"
  337. +"table.cnt,tr.cnt,td.cnt,tbody.cnt,div.pyl,pre.pyl,p.pyl {border:0 none;border-top:0;border-right:0;border-bottom:0;border-left:0;padding:0px; margin:0px;font-family:Verdana,Helvetica,Arial,sans-serif;font-size:16px; font-size-adjust:none;font-stretch:normal;font-style:normal;font-variant:normal;font-weight:normal;line-height:normal;}"
  338. +"table.cnt,td.cnt,div.pyl,pre.pyl,sup.pyl,a.pyl,p.pyl {color:black;background:#F9F9F9;text-decoration: none;}"
  339. +"a.pyl:hover{text-decoration: underline;}"
  340. +"table.cnt,div.pyl,pre.pyl {width:auto;}"
  341. +"pre.pyl,div.pyl {overflow:visible;}"
  342. +"div.pyl {position: relative; float: left; padding-left: 3px; padding-right: 3px; height: 55px;}"
  343. +"</style>";
  344.             var doc = wordschinesepopupdictionary.currDoc;
  345.             if(!doc) doc=content.document;
  346.             var div = doc.getElementById(wordschinesepopupdictionary.divId);
  347.             if(div){
  348.                 if(wordschinesepopupdictionary.isCN){
  349.                     var mhtml="";
  350.                     mhtml+=style;
  351.                     if(wordschinesepopupdictionary.retGG!=""){
  352.                         mhtml+="<table class=\"cnt\"><tr><td class=\"cnt\" style=\"padding: 5px;color: grey;\">";
  353.                         mhtml+=wordschinesepopupdictionary.retGG;
  354.                         mhtml+="</td></tr></table>";
  355.                     }
  356.  
  357.                     mhtml+=wordschinesepopupdictionary.retWC;
  358.                     div.innerHTML=mhtml;
  359.                 }else{
  360.                     if(wordschinesepopupdictionary.retWC){
  361.                         div.innerHTML=style+wordschinesepopupdictionary.retWC;
  362.                     }else{
  363.                         div.innerHTML=style+"<table class=\"cnt\"><tr><td class=\"cnt\">"+wordschinesepopupdictionary.retGG+"</td></tr></table>";
  364.                     }
  365.                 }
  366.             }
  367.     },
  368.     
  369.  
  370.  
  371.     doRequest: function (url){
  372.         url=encodeURI(url);
  373.  
  374.         var req = new XMLHttpRequest();
  375.         req.open('GET', url, true);
  376.         req.onreadystatechange = function (aEvt) {
  377.             if (req.readyState == 4) {
  378.                 if(req.status == 200) {
  379.                     var text = req.responseText;
  380.                     wordschinesepopupdictionary.retWC = text;
  381.                     wordschinesepopupdictionary.setDiv();
  382.                 }
  383.             }
  384.         };
  385.         req.send(null);
  386.     },
  387.     
  388.  
  389.  
  390.     googleTranslate: function(text2translate,target) {
  391.          if (text2translate.length>0 && text2translate.length<=1100) {
  392.     
  393.             var httpRequest = null;
  394.             var fullUrl = "http://translate.google.com/translate_t?text="+text2translate+"&hl="+target+"&langpair=auto|"+target+"&tbb=1" ;
  395.             function removeHTMLTags(mitkell) {  //clean up a string from html tags
  396.                 var strInputCode = mitkell;
  397.                 var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
  398.                 return strTagStrippedText;
  399.             }
  400.         
  401.             function infoReceived() {  // if there is response from Google then write out translation
  402.                 var output = httpRequest.responseText;
  403.      
  404.                 if (output.length) {
  405.                  // Build the output string from Google Page
  406.                              output = output.replace(/"/gi,'"');
  407.                              output = output.replace(/</gi,'<');
  408.                              output = output.replace(/>/gi,'>');
  409.                              output = output.replace(/&/gi,'&');
  410.                              output = output.replace(/'/gi,"'");
  411.                  
  412.                     var fieldArray = output.split('</head>');
  413.                     if (fieldArray[1].search('class="short_text"')!=-1) {
  414.                         var tempResz = fieldArray[1].split('<span id=result_box class="short_text">');
  415.                     }else if (fieldArray[1].search('class="medium_text"')!=-1) {
  416.                         var tempResz = fieldArray[1].split('<span id=result_box class="medium_text">');
  417.                     }else {
  418.                         var tempResz = fieldArray[1].split('<span id=result_box class="long_text">');
  419.                     }
  420.                     
  421.                     var kimenet = tempResz[1].split('</div>');
  422.                     var transtedText = removeHTMLTags(kimenet[0]);
  423.                     var py = "";
  424.                     if(!wordschinesepopupdictionary.isCN){
  425.                         py = removeHTMLTags(kimenet[1]);
  426.                         py += "<br>";
  427.                     }
  428.  
  429.                     wordschinesepopupdictionary.retGG = py+transtedText;
  430.                     wordschinesepopupdictionary.setDiv();
  431.  
  432.                     if(!wordschinesepopupdictionary.isCN){
  433.                         url = wordschinesepopupdictionary.url+"?hs=1&lang="+wordschinesepopupdictionary.langId+"&q="+transtedText;
  434.                         url=url.replace("q=#","q=");
  435.                         wordschinesepopupdictionary.doRequest(url);
  436.                     }
  437.                     
  438.                 }
  439.             }
  440.             
  441.             httpRequest = new XMLHttpRequest();
  442.             httpRequest.open("GET", fullUrl, true);
  443.             httpRequest.onload = infoReceived;
  444.             httpRequest.send(null);
  445.         }else { //if text length<=0 or >1100
  446.             alert(this.strings.getString("errorMessageOnWrongTextLength"));
  447.         }
  448.     },
  449.  
  450. };
  451. window.addEventListener("load", function(e) { wordschinesepopupdictionary.load(e); }, false);
  452. window.addEventListener("unload", function(e) { wordschinesepopupdictionary.unload(e); }, false);